<?php
//链接数据库
//对数据进行操作
  class Db{
      //属性
      private $host;
      private $root;
      private $password;
      private $datebase;
      private $port;
      public function __construct($root,$password,$datebase,$host="localhost",$port='3306'){
          //  $this->host=isset($host)?$host:"localhost";
          //  $this->root=isset($root)?$root:'root';
          //  $this->password=isset($password)?$password:'root';
          //  $this->data=$datebase;
          //  $this->port=isset($port)?$port:$port;
          $this->host=$host;
          $this->root=$root;
          $this->password=$password;
          $this->data=$datebase;
          $this->port=$port;
          $this->connectDb();
          
      }
      public function connectDb(){
        $this->conn=mysqli_connect(
          $this->host,
          $this->root,
          $this->password,
          $this->datebase
          // $this->port
        );
        if($this->conn){
          echo "数据库链接成功";
        }else{
          echo "数据库链接失败",mysqli_error($this->conn);
          return;
        }
      }

      //增加数据
      public function write($n,$a){
        $sql="INSERT INTO test.user(uname,age) VALUES('".$n."',".$a.")";
        if(mysqli_query($this->conn,$sql)){
          echo "插入成功";
        }else{
          echo "插入失败".mysqli_error($this->conn);
        }
      }

      //修改数据
      public function upDate($un,$ua,$uid){
        $sql=" UPDATE test.user SET uname='".$un."',age=".$ua." WHERE id=".$uid;
        if(mysqli_query($this->conn,$sql)){
          echo "修改成功";
        }else{
          echo "修改失败".mysqli_error($this->conn);
        }
      }
      //删除数据
      public function Delete($did){
        $sql="DELETE FROM test.user WHERE id=".$did;
        if(mysqli_query($this->conn,$sql)){
          echo "删除成功";
        }else{
          echo "删除失败".mysqli_error($this->conn);
        }
      }

      //查询数据
      public function Select($databasename){
        $sql="SELECT * FROM ".$databasename;
        if(mysqli_query($this->conn,$sql)){
          echo "查询成功";
          // echo "姓名：".$sql['uname']."年龄：".$sql['age'];
        }else{
          echo "查询失败".mysqli_error($this->conn);
        }
      }
    }
  $db=new Db("root","root","test");
  
  // $db->upDate('喵喵喵',12,1);
  $db->Select('test.user');
  // $db->Delete(3);

  // var_dump($db);
  
?>